import re
import string


from xml.dom import minidom, Node

class SVGParser:
    def __init__(self,xmlString,factor=0):
        
        self.dotSceneXML = minidom.parseString(xmlString)
         
        svgNodes = self.dotSceneXML.getElementsByTagName('svg')
        
        if not svgNodes:
            print "ERROR : not an SVG file !"
            return 
        
        if len(svgNodes)<1:
            print "ERROR : not SVG node !"
            return 
        
        svg=svgNodes[0]
        
        w=svg.getAttribute('width')
        h=svg.getAttribute('height')
        
        if ((not w) or (not h)):
            print "Error, incompatible format (dim)"
            return 
        
        self.dimensions=(int(string.atoi(w)*factor),int(string.atoi(h)*factor))
        
        self.basePos=(0,0)
        self.factor=factor
        self.levelName="N/A"
        self.ore=[]
        
        if factor==0:
            levelInfoNode=self.dotSceneXML.getElementsByTagName('level')[0]
            self.levelName=levelInfoNode.getAttribute('name')
            
            factor=self.factor=self.getFloatAttribute(levelInfoNode,'scale')
            
            self.basePos=(0,0)
            
            base=levelInfoNode.getElementsByTagName('base')[0]
            self.basePos=(  self.getFloatAttribute(base,'x')*factor, \
                            self.getFloatAttribute(base,'y')*factor)
            
            
            for on in levelInfoNode.getElementsByTagName('ore'):
                pos_x=self.getFloatAttribute(on,'x')*factor
                pos_y=self.getFloatAttribute(on,'y')*factor
                v=self.getFloatAttribute(on,'value')
                self.ore.append(((pos_x,pos_y),v))        
            
            
        self.pathNodes = self.dotSceneXML.getElementsByTagName('path')
        
                            
        self.paths=self.decode()
        
        self.positions= self.decodePos(self.dotSceneXML.getElementsByTagName('rect'))
   
        
        
    def getDimensions(self):
        return self.dimensions
    
    def getScale(self):
        return self.factor
    
    def getFloatAttribute(self,node,attr):
        return string.atof(node.getAttribute(attr))
    
    def getBasePosition(self):
        return self.basePos
    
    def getLevelName(self):
        return self.levelName
    
    def getOre(self):
        return self.ore
    
    def getPaths(self):
        return self.paths
    
    def getPath(self,i):
        if 0<i<len(self.paths):
            return self.paths[i]
        else:
            print "Illegal index "+str(i)
            return None
            
    def test(self,s):
        res=decodePoints(s)
        if res:
            for s in res:
                print str(s[0])+" / "+str(s[1])

    def printPaths(self):
        for p in self.paths:
            print p
    def decode(self):
        paths=[]
        for node in self.pathNodes:
            s=node.getAttribute('d')
            if s:
                pts=self.decodePoints(s)
                if pts:
                    paths.append(pts)
        return paths        
    
    def decodePos(self,nodes):
        pos={}
        for node in nodes:
            s=node.getAttribute('id')
            if s:
                
                x=string.atof(node.getAttribute('x'))*self.factor
                y=string.atof(node.getAttribute('y'))*self.factor
                
                pos[s]=(x,y)
                
        return pos        
                            
    def decodePoints(self,s):
        pat = r'[A-Z] ([-+]?\d+\.\d*)\,([-+]?\d+\.\d*)'
        
        r = re.compile(pat)      # Compile it
        m = r.findall(s)           # See if string s matches

        res=[]
        if m:
            for x in m :
                res.append((string.atof(x[0])*self.factor,string.atof(x[1])*self.factor))
            return tuple(res)
        else:
            return None


if __name__ == '__main__':
    
    #doTest("M 371.64703,299.72324 L 354.42364,310.34797 L 339.84807,311.74654 L 326.01678,311.02311 L 311.02621,315.28181 L 295.62476,328.24884 L 288.39915,341.43866 L 284.62225,351.84207 L 279.56697,356.44986 L 265.63528,351.6739 L 254.16662,339.01952 L 242.14183,325.71364 L 226.54171,318.98318 L 192.31081,322.22791 L 179.11686,332.75819 L 175.93229,343.71 L 171.72956,348.21936 L 153.11329,351.93449 L 131.77683,357.8639 L 115.94927,352.99342 L 113.85967,324.30887 L 111.36655,312.46627 L 103.10506,301.19845 L 94.087212,289.95249 L 89.325031,278.17551 L 89.606257,262.52662 L 92.579964,246.13152 L 97.923686,230.18865 L 105.31496,215.89649 L 116.41381,201.69049 L 126.08833,192.52135 L 132.64731,185.48826 L 134.39958,177.69042 L 130.34743,162.74738 L 123.41616,148.04131 L 115.10448,131.02552 L 106.91108,109.15334 L 104.06503,92.999526 L 104.22592,74.851351 L 107.95265,59.03696 L 115.80416,49.884496 L 140.62829,42.742307 L 155.77214,44.167583 L 166.33228,47.647114 L 177.40526,46.667686 L 183.2115,40.367584 L 187.24067,30.347932 L 191.26593,20.903719 L 197.06043,16.329937 L 214.28761,18.531412 L 233.00529,25.541197 L 252.27654,33.250661 L 271.16439,37.551171 L 284.17238,34.271179 L 288.6945,26.389723 L 292.88493,20.841307 L 304.89784,24.560436 L 319.54445,34.967347 L 334.15207,49.501224 L 346.00203,66.746095 L 352.37568,85.285991 L 358.54799,94.328361 L 369.4292,94.290508 L 380.51533,92.079971 L 387.30237,94.604289 L 389.13271,106.81668 L 385.43234,117.692 L 381.23832,131.76954 L 381.58768,153.58857 L 387.66233,161.13846 L 399.01095,168.85572 L 409.45059,180.46905 L 412.7983,199.70713 L 407.57154,216.22074 L 396.75595,233.09297 L 383.19104,249.25212 L 369.71632,263.62646 L 361.01455,278.86599 L 364.45199,288.57998 L 371.00427,294.8414 L 371.64703,299.72324 z ")
    svg=SVGParser("drawing.svg",1.0)
    print svg.getPath(1)
    print svg.getPath(8)
    
    
